'use strict';
const extend = require('extend');
const logger = require('log4js').getLogger("sys");

const CommonService = require("../../service/util/dbService");
const Model = require("../../module/officialAccount/OfficialAccountModel");
const WechatUtil = require('../../module/util/WechatUtil');

const defaultParams = {
    model : Model,
    basePath: '/officialAccount',
};

class ModuleService extends CommonService{
    constructor(param){
        super(param);
        this.opts = extend(true, {}, this.opts, defaultParams, param);
        this._name = "OfficialAccountService";
    }
    updateById(curUser, id = "", data = {}) {
        const thisService = this,
            superThisFunc = super.updateById;
        let promise;

        promise = new Promise(function (resolve, reject) {
            superThisFunc.call(thisService, curUser, id, data)
                .then(function (result) {
                    global.cache.officialAccount[result._id].resetOptions(result._doc || result);
                    resolve(result);
                }, function (err) {
                    reject(err);
                });
        });

        return promise;
    }
    save(curUser, data = {}) {
        const thisService = this,
            superThisFunc = super.save;
        let promise;

        promise = new Promise(function (resolve, reject) {
            superThisFunc.call(thisService, curUser, data).then(function (result) {
                global.cache.officialAccount[result._id] = new WechatUtil(result._doc || result);
                resolve(result)
            }, function (err) {
                reject(err);
            });
        });

        return promise;
    }
}

module.exports = new ModuleService();